import os
from flask import Flask, render_template, g, request
from .blueprint import api
from . import mserver
import humanize
import re
import mimetypes
from datetime import datetime

ignored = ['.bzr', '$RECYCLE.BIN', '.DAV', '.DS_Store', '.git', '.hg', '.htaccess', '.htpasswd', '.Spotlight-V100',
           '.svn', '__MACOSX', 'ehthumbs.db', 'robots.txt', 'Thumbs.db', 'thumbs.tps']
datatypes = {'audio': 'm4a,mp3,oga,ogg,webma,wav', 'archive': '7z,zip,rar,gz,tar',
             'image': 'gif,ico,jpe,jpeg,jpg,png,svg,webp', 'pdf': 'pdf', 'quicktime': '3g2,3gp,3gp2,3gpp,mov,qt',
             'source': 'atom,bat,bash,c,cmd,coffee,css,hml,js,json,java,less,markdown,md,php,pl,py,rb,rss,sass,scpt,swift,scss,sh,xml,yml,plist',
             'text': 'txt', 'video': 'mp4,m4v,ogv,webm', 'website': 'htm,html,mhtm,mhtml,xhtm,xhtml'}
icontypes = {'fa-music': 'm4a,mp3,oga,ogg,webma,wav', 'fa-archive': '7z,zip,rar,gz,tar',
             'fa-picture-o': 'gif,ico,jpe,jpeg,jpg,png,svg,webp', 'fa-file-text': 'pdf',
             'fa-film': '3g2,3gp,3gp2,3gpp,mov,qt',
             'fa-code': 'atom,plist,bat,bash,c,cmd,coffee,css,hml,js,json,java,less,markdown,md,php,pl,py,rb,rss,sass,scpt,swift,scss,sh,xml,yml',
             'fa-file-text-o': 'txt', 'fa-film': 'mp4,m4v,ogv,webm', 'fa-globe': 'htm,html,mhtm,mhtml,xhtm,xhtml'}



def create_app():
    bind = os.getenv('FS_BIND', '0.0.0.0')
    port = os.getenv('FS_PORT', '8000')
    root = os.path.normpath(os.getenv('FS_PATH', 'C:\\Users\\yuang'))
    key = os.getenv('FS_KEY')
    app = Flask(__name__)
    app.register_blueprint(api.api,url_prefix='/api')
    path_view = mserver.PathView.as_view('path_view')
    app.add_url_rule('/',view_func=path_view)
    app.add_url_rule("/<path:p>",view_func=path_view)
    # -------------------------------------------------
    @app.template_filter('size_fmt')
    def size_fmt(size):
        return humanize.naturalsize(size)

    @app.template_filter('time_fmt')
    def time_desc(timestamp):
        mdate = datetime.fromtimestamp(timestamp)
        str = mdate.strftime('%Y-%m-%d %H:%M:%S')
        return str

    @app.template_filter('data_fmt')
    def data_fmt(filename):
        t = 'unknown'
        for type, exts in datatypes.items():
            if filename.split('.')[-1] in exts:
                t = type
        return t

    @app.template_filter('icon_fmt')
    def icon_fmt(filename):
        i = 'fa-file-o'
        for icon, exts in icontypes.items():
            if filename.split('.')[-1] in exts:
                i = icon
        return i

    @app.template_filter('humanize')
    def time_humanize(timestamp):
        mdate = datetime.utcfromtimestamp(timestamp)
        return humanize.naturaltime(mdate)
    # ----------------------------------------------------
    return app

app = create_app()
if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8000)